package com.zone.editor.photoEditor;

import android.view.View;

import com.zone.editor.inter.EditorViewState;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Tracked state of user-added views (stickers, emoji, text, etc)
 */
public class PhotoEditorViewState implements EditorViewState {

    private View currentSelectedView;
    private List<View> addedViews;
    private Stack<View> redoViews;

    public PhotoEditorViewState() {
        this.currentSelectedView = null;
        this.addedViews = new ArrayList<>();
        this.redoViews = new Stack<>();
    }

    @Override
    public View getCurrentSelectedView() {
        return currentSelectedView;
    }
    @Override
    public void setCurrentSelectedView(View currentSelectedView) {
        this.currentSelectedView = currentSelectedView;
    }
    @Override
    public void clearCurrentSelectedView() {
        this.currentSelectedView = null;
    }
    @Override
    public View getAddedView(int index) {
        return addedViews.get(index);
    }
    @Override
    public int getAddedViewsCount() {
        return addedViews.size();
    }
    @Override
    public void clearAddedViews() {
        addedViews.clear();
    }
    @Override
    public void addAddedView(final View view) {
        addedViews.add(view);
    }
    @Override
    public void removeAddedView(final View view) {
        addedViews.remove(view);
    }
    @Override
    public View removeAddedView(final int index) {
        return addedViews.remove(index);
    }
    @Override
    public boolean containsAddedView(final View view) {
        return addedViews.contains(view);
    }

    /**
     * Replaces a view in the current "added views" list.
     *
     * @param view The view to replace
     * @return true if the view was found and replaced, false if the view was not found
     */
    @Override
    public boolean replaceAddedView(final View view) {
        final int i = addedViews.indexOf(view);
        if (i > -1) {
            addedViews.set(i, view);
            return true;
        }
        return false;
    }
    @Override
    public void clearRedoViews() {
        redoViews.clear();
    }
    @Override
    public void pushRedoView(final View view) {
        redoViews.push(view);
    }
    @Override
    public View popRedoView() {
        return redoViews.pop();
    }
    @Override
    public int getRedoViewsCount() {
        return redoViews.size();
    }
    @Override
    public View getRedoView(int index) {
        return redoViews.get(index);
    }
}
